package com.yuanhua.rabbitmq.helloworld;

import com.rabbitmq.client.*;
import com.sun.corba.se.impl.util.RepositoryIdCache;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("");
        connectionFactory.setPort(222);
        connectionFactory.setUsername("55");
        connectionFactory.setPassword("52300");
        connectionFactory.setVirtualHost("/yuanhua");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("queue1",false,false,false,null);

        //从mq服务中获取数据
        channel.basicConsume("queue1",false, new Reciver(channel));
    }
}

class Reciver extends DefaultConsumer {

    private Channel channel;
    public Reciver(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

        String msg = new String(body);
        System.out.println(msg);

        //确认消息,false确认当前消息；
        channel.basicAck(envelope.getDeliveryTag(), false);
    }
}